package de.dlrg.wms.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import wms2010.interfaces.devices.IDevice;

import com.google.code.trapo.validation.Unique;

import de.dlrg.wms.model.NullClasses.AgeClassNull;

@javax.persistence.Entity
public class Run {
	private static final long serialVersionUID = -7861732087418717397L;
	

	public boolean checkAllSwimmerStopped() {
		boolean ret = true;
		for (SwimmerTrackRun swimmerTrackRun : this.getSwimmerTrackRun()) {
			ret &= swimmerTrackRun.isStopped();
		}
		return ret;
	}

	public boolean checkAllSwimmerReady() {
		boolean ret = true;
		for (SwimmerTrackRun swimmerTrackRun : this.getSwimmerTrackRun()) {
			ret &= swimmerTrackRun.isReady();
		}
		return ret;
	}
	
	@Size(min = 1)
	@OneToMany(fetch = FetchType.EAGER)
	private List<SwimmerTrackRun> swimmerTrackRun = new ArrayList<SwimmerTrackRun>();
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private Integer runNo;
	private Long startTime;

	public Long getStartTime() {
		return startTime;
	}

	public void setStartTime(Long startTime) {
		this.startTime = startTime;
	}

	public Integer getRunNo() {
		return runNo;
	}

	public Run setRunNo(Integer runNo) {
		this.runNo = runNo;
		return this;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<SwimmerTrackRun> getSwimmerTrackRun() {
		return swimmerTrackRun;
	}

	public void setSwimmerTrackRun(List<SwimmerTrackRun> swimmerTrackRun) {
		this.swimmerTrackRun = swimmerTrackRun;
	}

	

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Run) {
			Run run = (Run) obj;
			boolean ret = (run.getId().equals(this.getId()));
			return ret;
		}
		return super.equals(obj);
	}
	
	
	public SwimmerTrackRun getSwimmerByTrack(Track track) {
		for(SwimmerTrackRun swimmerTrackRun : this.swimmerTrackRun) {
			if(swimmerTrackRun.getTrack().equals(track)) {
				return swimmerTrackRun;
			}
		}
		return null;
	}
}
